/*
 * Copyright (c) 2012-2013, CS Bakery. All rights reserved.
 */

package testCase;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Vector;

import org.junit.Test;
import junit.framework.TestCase;
import csBakery.*;

/*
 * 	@release Final Release
 *	@author  Lee Cheuk Wang
 *	@version 1.4.3
 *
 * 	@release Release 2
 *	@author  Leung Ho Yan
 *	@version 1.4.2
 *
 * 	@release Release 1
 *	@author  So Mo Kin
 *	@version 1.0.0
 */

public class StockTest extends TestCase {

	private StockController sc;
	private Stock s;
	private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
	private final String testfile = "test\\stocktest.txt";
	 
	public void setUp(){
		sc = new StockController(testfile);
		s = sc.getStocks();
		s.setProductQuantity("Bagel", 10);
		s.setProductQuantity("Baguette", 20);
		
		//Set up for output stream testing
		System.setOut(new PrintStream(outContent));
	}
	public void tearDown()	{
		System.setOut(null);
	}
	//get the product set up successfully
	@Test 
	public void testGetStockQuantityCase1()	{ 
		assertEquals(10,s.getProductQuantity("Bagel"));
		assertEquals(20,s.getProductQuantity("Baguette"));
	}
	//Failed to get not exist stock
	@Test 
	public void testGetStockQuantityCase2()	{ 
		assertEquals(-1,s.getProductQuantity("Bag"));
	}
	
	//Set the stock successfully
	@Test 
	public void testSetStockQuantityCase1()	{ 
		s.setProductQuantity("Bagel", 20);
		s.setProductQuantity("Baguette", 40);
		assertEquals(20,s.getProductQuantity("Bagel"));
		assertEquals(40,s.getProductQuantity("Baguette"));
	}
	
	//Failed to set for stock unidentified
	@Test 
	public void testSetStockQuantityCase2()	{ 
		s.setProductQuantity("Bag", 20);
		assertEquals(-1,s.getProductQuantity("Bag"));
	}
	
	//get the product list successfully
	@Test 
	public void testGetProductList(){
		Vector<String> vector = new Vector<String>(2);
		vector.add("Bagel");
		vector.add("Baguette");
		assertEquals(vector, s.getStockProductList());
	}
	
	//get the product info successfully
	@Test 
	public void testGetProductInfoCase1(){
		Product bagel = new Product("Bagel", 10);
		Product baguette = new Product("Baguette", 20);
		
		assertEquals(bagel.getName(), s.getProductInformation("Bagel").getName());
		assertEquals(bagel.getQuantity(), s.getProductInformation("Bagel").getQuantity());
		assertEquals(baguette.getName(), s.getProductInformation("Baguette").getName());
		assertEquals(baguette.getQuantity(), s.getProductInformation("Baguette").getQuantity());
	}
	
	//fail to get not exist product info
	@Test 
	public void testGetProductInfoCase2(){
		s.getProductInformation("Bag");
		String output = "Product not found.";
		assertEquals(output, outContent.toString());
	}
	
	//test the to string have output or not
	@Test
	public void testToString(){
		String output = "";
		output += "Bagel 10\n";
		output += "Baguette 20\n";
		assertEquals(output, s.toString());
	}
	
	 

}
